#! python3
# -*- coding: utf-8 -*-

import requests
import re
import os

urls = 'http://www.169we.com'
headers = {

    # "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36"
    "User-Agent":"Mozilla/5.0 (windows NT 10.0;win64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134"
}

#url请求函数
def url_get(urls):
    req = requests.get(urls,headers)#向目标发送 get 请求返回一个 requests 对象
    req.encoding = 'gb2312'
    return req.text

#分析提取数据
def html_parse(soup):
    pattern_jpg = re.compile('<img src="(.+?)" .*?>''',re.S) #提取图片
    pattern_cn = re.compile(r'[\u4E00-\u9FA5]+') #提取中文
    herf = list(set(re.findall(pattern_jpg, soup)))
    return herf

def url_new(soup): #提取 url 未编辑
    pattern_href = re.compile(r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')",re.S) #正则对象，提取 href
    url = list(set(re.findall(pattern_href, soup)))
    return url

#url管理器,未编辑,如果 url 不在新建列表中就把他添加到列表
def url_manager(url):
    news_url = []
    for urls in url:
        if urls not in news_url:
            news_url.append(urls)
    return news_url

#存储数据
def html_save(html):
    path = 'tupian' #文件夹名称
    num = 1 #用来编辑图片名字
    if not os.path.exists(path):
        os.makedirs(path)
        print('新建文件夹' + path)
    for i in html:
        num += 1
        response = requests.get(i).content
        filenname = path + '/' + str(num) + '.jpg' #将图片倒数第二个字符串作为文件名
        print('保存图片：%s' % filenname)
        with open(filenname,'wb') as f:
            f.write(response)
            f.close()

if __name__ == "__main__":
    urls = url_get(urls)#测试代码
    urlslist = html_parse(urls)
    html_save(urlslist)
    print('运行完毕！')
